package com.antdesignbackend.backend.common;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by luyangjia on 2020/2/27.
 */
public class CalendarDate {
    public static String getDate(String type)
    {
        String result="";
        java.util.Calendar curStartCal = java.util.Calendar.getInstance();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        java.util.Calendar calendar = (java.util.Calendar) curStartCal.clone();
        switch(type)
        {
            case "today":
                result = format.format(calendar.getTime()); //当前时间
                break;
            case "yesterday":
                calendar.add(Calendar.DATE, -1);//昨日时间
                result = format.format(calendar.getTime());
                break;
            case "tomorrow":
                calendar.add(Calendar.DATE, 1);//昨日时间
                result = format.format(calendar.getTime());
                break;
            case "weekFist":
                calendar.set(java.util.Calendar.DAY_OF_WEEK, java.util.Calendar.MONDAY);
                result = format.format(calendar.getTime()); //本周周一时间
                break;
            case "weekLast":
                calendar.set(java.util.Calendar.DAY_OF_WEEK, java.util.Calendar.MONDAY);
                calendar.add(java.util.Calendar.DATE, 6);
                result = format.format(calendar.getTime()); //本周周日时间
                break;
            case "monthFirst":
                calendar.set(java.util.Calendar.DAY_OF_MONTH, calendar.getActualMinimum(java.util.Calendar.DAY_OF_MONTH));
                result = format.format(calendar.getTime()); //本月第一天时间
                break;
            case "monthLast":
                calendar.set(java.util.Calendar.DAY_OF_MONTH, calendar.getActualMaximum(java.util.Calendar.DAY_OF_MONTH));
                result = format.format(calendar.getTime());//本月最后一天时间
                break;
            default:
                break;
        }
        return result;
    }
    /**
     **pattern: "yyyy-MM-dd HH:mm:ss"
     **/
    public static Date strToDate(String str,String pattern) {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        Date date = null;
        try {
            date = format.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     **pattern: "yyyy-MM-dd HH:mm:ss"
     **/
    public static String dateToStr(Date date, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date.getTime());
    }
    /**
     **日期加分钟、小时、天
     **/
    public static Date addDate(Date date,String type,Integer value) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        switch (type)
        {
            case "min":
                //分钟
                calendar.add(Calendar.MINUTE, value);// 分钟
                break;
            case "hour":
                calendar.add(Calendar.HOUR, value);// 24小时制
                break;
            case "day":
                calendar.add(Calendar.DAY_OF_MONTH, value);
                break;
            case "month":
                calendar.add(Calendar.MONTH, value);
                break;
            case "year":
                calendar.add(Calendar.YEAR, value);
                break;
            default:
                break;
        }

        date = calendar.getTime();
        return date;
    }

    /**
     * 两个时间之间相差距离多少天
     *
     * @param str1 时间参数 1：
     * @param str2 时间参数 2：
     * @return 相差天数
     */
    public static long getDistanceDays(String str1, String str2) throws Exception {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date one;
        Date two;
        long days = 0;
        try {
            one = df.parse(str1);
            two = df.parse(str2);
            long time1 = one.getTime();
            long time2 = two.getTime();
            long diff;
            if (time1 < time2) {
                diff = time2 - time1;
            } else {
                diff = time1 - time2;
            }
            days = diff / (1000 * 60 * 60 * 24);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return days;
    }

    /**
     * 两个时间相差距离多少天多少小时多少分多少秒
     *
     * @param str1 时间参数 1 格式：1990-01-01 12:00:00
     * @param str2 时间参数 2 格式：2009-01-01 12:00:00
     * @return long[] 返回值为：{天, 时, 分, 秒}
     */
    public static long[] getDistanceTimes(String str1, String str2) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date one;
        Date two;
        long day = 0;
        long hour = 0;
        long min = 0;
        long sec = 0;
        try {
            one = df.parse(str1);
            two = df.parse(str2);
            long time1 = one.getTime();
            long time2 = two.getTime();
            long diff;
            if (time1 < time2) {
                diff = time2 - time1;
            } else {
                diff = time1 - time2;
            }
            day = diff / (24 * 60 * 60 * 1000);
            hour = (diff / (60 * 60 * 1000) - day * 24);
            min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
            sec = (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long[] times = {day, hour, min, sec};
        return times;
    }

    /**
     * 两个时间相差距离多少天多少小时多少分多少秒
     *
     * @param str1 时间参数 1 格式：1990-01-01 12:00:00
     * @param str2 时间参数 2 格式：2009-01-01 12:00:00
     * @return String 返回值为：xx天xx小时xx分xx秒
     */
    public static String getDistanceTime(String str1, String str2) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date one;
        Date two;
        long day = 0;
        long hour = 0;
        long min = 0;
        long sec = 0;
        try {
            one = df.parse(str1);
            two = df.parse(str2);
            long time1 = one.getTime();
            long time2 = two.getTime();
            long diff;
            if (time1 < time2) {
                diff = time2 - time1;
            } else {
                diff = time1 - time2;
            }
            day = diff / (24 * 60 * 60 * 1000);
            hour = (diff / (60 * 60 * 1000) - day * 24);
            min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
            sec = (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return day + "天" + hour + "小时" + min + "分" + sec + "秒";
    }

    /**
     * 获取精确到秒的时间戳
     * @return
     */
    public static int getSecondTimestamp(Date date){
        if (null == date) {
            return 0;
        }
        String timestamp = String.valueOf(date.getTime());
        int length = timestamp.length();
        if (length > 3) {
            return Integer.valueOf(timestamp.substring(0,length-3));
        } else {
            return 0;
        }
    }

    public static Long dateDiff(String startTime, String endTime,String type) {
        long result=0;
        DateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数
        long nh = 1000 * 60 * 60;// 一小时的毫秒数
        long nm = 1000 * 60;// 一分钟的毫秒数
        long ns = 1000;// 一秒钟的毫秒数
        long diff;
        long day = 0;
        long hour = 0;
        long min = 0;
        long sec = 0;
// 获得两个时间的毫秒时间差异
        try {
            diff = sd.parse(endTime).getTime() - sd.parse(startTime).getTime();
            //diff = startTime.getTime() - endTime.getTime();
            day = diff / nd;// 计算差多少天
            hour = diff % nd / nh + day * 24;// 计算差多少小时
            min = diff % nd % nh / nm + day * 24 * 60;// 计算差多少分钟
            sec = diff % nd % nh % nm / ns;// 计算差多少秒
            switch(type)
            {
                case "day":
                    if(hour>0 || min>0)
                        day=day+1;
                      result = day;
                    break;
                case "hour":
                    if(min>0)
                        hour=hour+1;
                    result = day*24+hour;
                    break;
                case "min":
                    result = (day*24+hour)*60+min;
                    break;
                case "sec":
                    result = ((day*24+hour)*60+min)*60+sec;
                    break;
                default:
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
     return result;
    }

}
